# !/usr/bin/env python
# encoding: utf-8


"""
  @author: gaogao
  @file: InstGenSS_N7786C.py
  @time: 2022/2/22 15:43
  @desc:
"""

from ATS.instrumentDriver import InstGen
from utils.singleton_util import singleton


@singleton
class SS_N7786C(InstGen.SS):
    def __init__(self, name):
        super().__init__(name)

    def idn(self):
        return self.handler.ask("*IDN?")

    def __init_set(self):
        self.handler.write("*RST")
        self.handler.write("*cls")

    def reset(self):
        return self.handler.ask("SYST:PRES;*OPC?")

    def set_power_unit(self, unit=0):
        """
        设置偏振的power单位
        :param unit: 0:dbm 1:Watts
        :return:
        """
        self.handler.write(f":POLarimeter:POWer:UNIT {unit}")

    def set_trigger_config_mode(self, mode):
        """
        Sets the hardware trigger configuration with regard to Output and Input Trigger Connectors.
        0 or DISabled:
        1 or DEFault:
        2 or PASSthrough:
        3 or LOOPback:
        4 or SCRambler2polarimeter:
        5 or POLarimeter2scrambler:
        :param mode: 常用两种：2和4
        :return:
        """
        self.handler.write(f":TRIG:CONF {mode}")

    def trigger(self, mode=1):
        """
        Generates a hardware trigger.
        1 or NODEA: Is identical to a trigger at the Input Trigger Connector.
        2 or NODEB: Generates trigger at the Output Trigger Connector.
        生成硬件触发
        :param mode:
        :return:
        """
        self.handler.write(f"TRIG {mode}")

    def trigger_output_mode(self, tag="STF"):
        """
        Specifies when an output trigger is generated and arms the module.
        DISabled:
        AVGover:
        MEASure:
        MODulation:
        STFinished:
        SWFinished:
        SWSTarted:
        Never.
        :param tag:
        :return:
        """
        self.handler.write(f"TRIGger:OUTPut {tag}")

    def get_logged_value(self):
        """
        This command gets the number of already logged values. Will be 0, if logging finished
        :return:
        """
        return self.handler.ask("POL:SWE:SAMP:CURR?")

    def get_polarization_sweep_data(self):
        """
        This command returns the measurement result as float binblock.
        :return:
        None: returns 4,* dimensional array with S0, S1, S2, S3
        SOP: returns 4,* dimensional array with S0, S1, S2, S3
        NORMalized: returns 3,* dimensional array with S1, S2, S3
        """
        return self.handler.query_binary_values("POL:SWE:GET? NORM")

    def stop_logging_polcontroller(self):
        """
        停止logging和偏振器
        :return:
        """
        self.handler.write("POL:STOP")  # Stops logging
        self.handler.write("PCON:STOP")  # This command stops the Polcontroller.

    def set_external_trigger_delay(self):
        # # Define an external trigger delay after changing the SOP value Integer value (0-32766) 1 means 1/32 us
        self.handler.write("PCON:SEQ:HOLD 1")

    def get_opc_state(self):
        return self.handler.ask("*OPC?").strip()

    def set_stabilization_mode(self, mode):
        """
        Sets the Stabilizer Operating Mode. 设置稳偏模式
        :param mode:0/off;1/on
        :return:
        """
        self.handler.write(f"STAB:STAB {mode}")  #

    def set_polarization_value(self, s1, s2, s3):
        """
        设置稳偏模式
        设置偏振值,设置最大或者最小偏振值
        :param s1:
        :param s2:
        :param s3:
        :return:
        """
        self.handler.write(f"STAB:SOP {s1},{s2},{s3}")

    def set_random_points_stepwidth(self, points=20000, stepwidth=1000):
        """
        :param points:
        :param stepwidth:
        :return:
        """
        self.handler.write("PCON:SEQ:DCOM 1")
        self.handler.write("PCON:SEQ:RRAT 8")

        result = self.handler.ask(":PCONtroller:LOAD:ARB?")
        # self.handler.ask(f"PCON:GEN:RAND? {points},{stepwidth}")
        # self.handler.ask(f"PCON:SAVE:ARB?")
        self.handler.write("PCON:SEQ:HOLD 640")  # Define an external trigger delay after changing the SOP value
        self.handler.write("PCON:SEQ:SMOD 3")
        self.handler.write("PCON:REP 0")

    def set_sample_point_rate(self, points=20000, rate=8000, average_time=100):
        self.handler.write("POL:TRIG:INP SME")
        self.handler.write(f"POL:SWE:SAMP {points}")
        self.handler.write(f"POL:SWE:SRAT {rate}HZ,{average_time}US")
        
        self.handler.write("POL:SWE:LOOP 0")
        self.handler.write("POL:GAIN 4")

    def set_wave_length(self, wave_length=1310):
        """
        This command sets the current wavelength / start wavelength.
        :param wave_length:
        :return:
        """
        self.handler.write(f"POL:WAV {wave_length}NM")
        self.handler.write(f"POL:SWE:STEP 0")

    def start_polarimeter_logging(self):
        """
        This command starts the polarimeter logging.
        :param status:
        None: Starts logging as configured
        SOP: Starts logging and sets loop to 1
        SOPCONTINUOUS: Starts logging and sets loop to 0 (endless logging)
        :return:
        """
        # self.handler.write("POL:SWE:START SOP")
        self.handler.write(f"POL:SWE:START")

    def start_polarization_sequence(self):
        """
        Start the polarization sequence with the configured repetitions, repetition rate, and Sequence Mode
        :return:
        """
        self.handler.write("PCON:STAR")
        # 


    def save_arb_sequence(self):
        result = self.handler.ask(":PCON:SAVE:ARB?")
        return result


    def load_arb_sequence(self):
        result = self.handler.ask(":PCONtroller:LOAD:ARB?")
        return result
